﻿ using System;
 using System.Collections.Generic;
 using System.IO;


namespace Lon.Server
{
    public class CSMDayReportProvider : DataProcessProvider
    {

        FrontServerNode frontServer = null;

        public CSMDayReportProvider(FrontServerNode frontServer)
        {
            this.frontServer = frontServer;
        }

      
        protected override void ProcessCommandPackage(DataFrame pkg)
        {
            if (pkg.DataBuf[0] == 0x40)
            {
                QueryDayList(pkg);
            }
            else
            {
                QueryDayReport(pkg);
            }
           
        }

        private void QueryDayReport(DataFrame pkg)
        {
            MemoryStream ms = new MemoryStream(pkg.DataBuf);
            BinaryReader br = new BinaryReader(ms);
            ms.Seek(1, SeekOrigin.Begin);
            UInt32 date=br.ReadUInt32();
            MemoryStream dayReport=DataStoreService.ReadDayReport(pkg.DstId, date, pkg.MinorDataType);
            DataFrame df = new DataFrame(dayReport.ToArray());
            df.SrcId = pkg.DstId;
            df.DstId = pkg.SrcId;
            frontServer.SendMessage(df);
           
        }

        private void QueryDayList(DataFrame pkg)
        { 
 	           
        }

        protected override void ProcessDataPackage(DataFrame rPkg)
        {
             if(rPkg.DataBuf[0]!=0x41) return;
             MemoryStream ms = new MemoryStream(rPkg.DataBuf);
              BinaryReader br = new BinaryReader(ms);
              ms.Seek(1, SeekOrigin.Begin);
              UInt32 date=br.ReadUInt32();
              DataStoreService.SaveDayReport(rPkg.SrcId,date,rPkg.MinorDataType,rPkg.Buf);
           
        }
    }
}

